<template>
  <div class="review-page">
    <div class="header">
      <span class="back" @click="goBack">&lt;</span>
      <span class="title">审核申请</span>
      <span class="role-tag">工长</span>
    </div>
    <div class="search-bar">
      <input v-model="search" placeholder="搜索申请人姓名" />
      <span class="icon-search"></span>
      <span v-if="search" class="icon-clear" @click="search=''">×</span>
    </div>
    <div v-for="app in filteredApps" :key="app.id" class="app-card">
      <img :src="app.avatar" class="avatar" />
      <div class="info">
        <div class="row">
          <span class="name">{{ app.name }}</span>
          <span class="time">{{ app.time }}</span>
        </div>
        <div class="team">申请加入：{{ app.team }}</div>
        <div class="reason">{{ app.reason }}</div>
        <div class="actions" v-if="app.status==='pending'">
          <button class="approve" @click="approve(app)">通过</button>
          <button class="reject" @click="reject(app)">拒绝</button>
        </div>
        <div v-else class="result" :class="app.status">{{ app.status==='approved'?'已通过':'已拒绝' }}</div>
      </div>
    </div>
    <!-- 二次确认弹窗 -->
    <div v-if="showConfirm" class="confirm-mask">
      <div class="confirm-dialog">
        <div class="confirm-msg">确定要拒绝该申请吗？</div>
        <div class="confirm-btns">
          <button class="confirm-cancel" @click="cancelReject">取消</button>
          <button class="confirm-ok" @click="confirmReject">确认拒绝</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useTeamStore } from '../stores/TeamStatsStore.js'

const router = useRouter()
const teamStore = useTeamStore()
const search = ref('')
const filteredApps = computed(() =>
  teamStore.applications.filter(app => app.name.includes(search.value))
)

function approve(app) {
  app.status = 'approved'
  // 找到对应班组
  const team = teamStore.teams.find(t => t.name === app.team)
  if (team) {
    // 生成新成员id
    const newId = Math.max(0, ...team.memberList.map(m => m.id)) + 1
    team.memberList.push({
      id: newId,
      name: app.name,
      avatar: app.avatar,
      role: '组员',
      leader: false
    })
    team.members += 1
  }
}

// 二次确认相关
const showConfirm = ref(false)
const pendingRejectApp = ref(null)

function reject(app) {
  showConfirm.value = true
  pendingRejectApp.value = app
}

function confirmReject() {
  if (pendingRejectApp.value) {
    pendingRejectApp.value.status = 'rejected'
  }
  showConfirm.value = false
  pendingRejectApp.value = null
}

function cancelReject() {
  showConfirm.value = false
  pendingRejectApp.value = null
}

function goBack() {
  router.back()
}
</script>

<style scoped>
.review-page { background: #f7f8fa; min-height: 100vh; max-width: 375px; margin: 0 auto; box-sizing: border-box; padding-bottom: 24px; }
.header { display: flex; align-items: center; height: 48px; background: #fff; font-size: 18px; font-weight: bold; border-bottom: 1px solid #f0f0f0; padding: 0 16px; justify-content: space-between; }
.back { color: #333; font-size: 22px; cursor: pointer; }
.title { flex: 1; text-align: center; font-size: 17px; font-weight: 500; }
.role-tag { background: #409eff; color: #fff; border-radius: 12px; font-size: 14px; padding: 2px 14px; margin-left: 8px; }
.search-bar { display: flex; align-items: center; background: #f7f8fa; padding: 12px 16px 8px 16px; position: relative; }
.search-bar input { flex: 1; border: none; background: #f0f1f2; border-radius: 8px; padding: 8px 32px 8px 32px; font-size: 15px; outline: none; }
.icon-search { position: absolute; left: 28px; top: 20px; width: 16px; height: 16px; background: url('data:image/svg+xml;utf8,<svg width="16" height="16" fill="none" xmlns="http://www.w3.org/2000/svg"><circle cx="7" cy="7" r="6" stroke="%23888" stroke-width="2"/><path d="M12 12l2.5 2.5" stroke="%23888" stroke-width="2" stroke-linecap="round"/></svg>') center/contain no-repeat; }
.icon-clear { position: absolute; right: 28px; top: 20px; font-size: 18px; color: #bbb; cursor: pointer; }
.app-card { background: #fff; border-radius: 12px; box-shadow: 0 1px 4px #eee; margin: 0 16px 16px 16px; padding: 16px 14px 12px 14px; display: flex; gap: 12px; }
.avatar { width: 48px; height: 48px; border-radius: 50%; object-fit: cover; }
.info { flex: 1; display: flex; flex-direction: column; }
.row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 2px; }
.name { font-weight: bold; font-size: 16px; color: #222; }
.time { color: #888; font-size: 13px; }
.team { color: #409eff; font-size: 14px; margin-bottom: 2px; }
.reason { color: #444; font-size: 14px; margin-bottom: 8px; }
.actions { display: flex; gap: 12px; }
.approve { background: #67c23a; color: #fff; border: none; border-radius: 6px; padding: 6px 22px; font-size: 15px; cursor: pointer; }
.reject { background: #fff; color: #f45c5c; border: 1px solid #f45c5c; border-radius: 6px; padding: 6px 22px; font-size: 15px; cursor: pointer; }
.result { margin-top: 6px; font-size: 15px; font-weight: bold; }
.result.approved { color: #67c23a; }
.result.rejected { color: #f45c5c; }
.confirm-mask {
  position: fixed;
  left: 0; top: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.18);
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
}
.confirm-dialog {
  background: #fff;
  border-radius: 10px;
  padding: 28px 32px 18px 32px;
  box-shadow: 0 2px 16px #e6eaf3;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.confirm-msg {
  font-size: 18px;
  color: #222;
  margin-bottom: 18px;
}
.confirm-btns {
  display: flex;
  gap: 18px;
}
.confirm-cancel {
  background: #eee;
  color: #666;
  border: none;
  border-radius: 6px;
  padding: 8px 24px;
  font-size: 15px;
  cursor: pointer;
}
.confirm-ok {
  background: #f45ca4;
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 8px 24px;
  font-size: 15px;
  cursor: pointer;
}
</style> 